package com.imooc.distributedemo.resume.leetcode;

/**
 * leetcode205 同构字符串
 */
public class Solution205 {
    public static void main(String[] args) {
        System.out.println(isIsomorphic("egg", "add")); // true
        System.out.println(isIsomorphic("foo", "bar")); // false
        System.out.println(isIsomorphic("paper", "title")); // true
        System.out.println(isIsomorphic("foo", "faa")); // true

    }

    public static boolean isIsomorphic(String s, String t) {

        if(s==null||t==null){
            return false;
        }
        if(s.length() != t.length()){
            return false;
        }

        int len = s.length();
        char[] mapS = new char[256]; //用于映射s到tf
        char[] mapT = new char[256]; // 用于映射t到s
        for (int i = 0; i < len; i++) {
            char charS = s.charAt(i);
            char charT = t.charAt(i);
            // 如果charS已经有映射 判断和charT 是否一直
            if(mapS[charS]==0&&mapT[charT]==0){
                mapS[charS] = charT;
                mapT[charT] = charS;
            }else if(mapS[charS] != charT || mapT[charT] != charS){
                return false;
            }
        }
        return true;
    }
}
